home *** CD-ROM | disk | FTP | other *** search
/ Clickx 96 / Clickx 96.iso / software / tools / tool / xbmc-10.1.exe / system / shaders / yuv2rgb_basic.glsl < prev    next >
Encoding:
Text File  |  2011-03-08  |  3.6 KB  |  115 lines

  1. /*
  2.  *      Copyright (C) 2010 Team XBMC
  3.  *      http://www.xbmc.org
  4.  *
  5.  *  This Program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2, or (at your option)
  8.  *  any later version.
  9.  *
  10.  *  This Program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with XBMC; see the file COPYING.  If not, write to
  17.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *  http://www.gnu.org/copyleft/gpl.html
  19.  *
  20.  */
  21.  
  22. #if(XBMC_texture_rectangle)
  23. # extension GL_ARB_texture_rectangle : enable
  24. # define texture2D texture2DRect
  25. # define sampler2D sampler2DRect
  26. #endif
  27.  
  28. uniform sampler2D m_sampY;
  29. uniform sampler2D m_sampU;
  30. uniform sampler2D m_sampV;
  31. varying vec2      m_cordY;
  32. varying vec2      m_cordU;
  33. varying vec2      m_cordV;
  34.  
  35. uniform vec2      m_step;
  36.  
  37. uniform mat4      m_yuvmat;
  38.  
  39. uniform float     m_stretch;
  40.  
  41. vec2 stretch(vec2 pos)
  42. {
  43. #if (XBMC_STRETCH)
  44.   // our transform should map [0..1] to itself, with f(0) = 0, f(1) = 1, f(0.5) = 0.5, and f'(0.5) = b.
  45.   // a simple curve to do this is g(x) = b(x-0.5) + (1-b)2^(n-1)(x-0.5)^n + 0.5
  46.   // where the power preserves sign. n = 2 is the simplest non-linear case (required when b != 1)
  47.   #if(XBMC_texture_rectangle)
  48.     float x = (pos.x * m_step.x) - 0.5;
  49.     return vec2((mix(2.0 * x * abs(x), x, m_stretch) + 0.5) / m_step.x, pos.y);
  50.   #else
  51.     float x = pos.x - 0.5;
  52.     return vec2(mix(2.0 * x * abs(x), x, m_stretch) + 0.5, pos.y);
  53.   #endif
  54. #else
  55.   return pos;
  56. #endif
  57. }
  58.  
  59. void main()
  60. {
  61. #if defined(XBMC_YV12) || defined(XBMC_NV12)
  62.  
  63.   vec4 yuv, rgb;
  64.   yuv.rgba = vec4( texture2D(m_sampY, stretch(m_cordY)).r
  65.                  , texture2D(m_sampU, stretch(m_cordU)).g
  66.                  , texture2D(m_sampV, stretch(m_cordV)).a
  67.                  , 1.0 );
  68.  
  69.   rgb   = m_yuvmat * yuv;
  70.   rgb.a = gl_Color.a;
  71.   gl_FragColor = rgb;
  72.  
  73. #elif defined(XBMC_YUY2) || defined(XBMC_UYVY)
  74.  
  75. #if(XBMC_texture_rectangle)
  76.   vec2 stepxy = vec2(1.0, 1.0);
  77.   vec2 pos    = stretch(m_cordY);
  78.   pos         = vec2(pos.x - 0.25, pos.y);
  79.   vec2 f      = fract(pos);
  80. #else
  81.   vec2 stepxy = m_step;
  82.   vec2 pos    = stretch(m_cordY);
  83.   pos         = vec2(pos.x - stepxy.x * 0.25, pos.y);
  84.   vec2 f      = fract(pos / stepxy);
  85. #endif
  86.  
  87.   //y axis will be correctly interpolated by opengl
  88.   //x axis will not, so we grab two pixels at the center of two columns and interpolate ourselves
  89.   vec4 c1 = texture2D(m_sampY, vec2(pos.x + (0.5 - f.x) * stepxy.x, pos.y));
  90.   vec4 c2 = texture2D(m_sampY, vec2(pos.x + (1.5 - f.x) * stepxy.x, pos.y));
  91.  
  92.   /* each pixel has two Y subpixels and one UV subpixel
  93.      YUV  Y  YUV
  94.      check if we're left or right of the middle Y subpixel and interpolate accordingly*/
  95. #ifdef XBMC_YUY2 //BGRA = YUYV
  96.   float leftY   = mix(c1.b, c1.r, f.x * 2.0);
  97.   float rightY  = mix(c1.r, c2.b, f.x * 2.0 - 1.0);
  98.   vec2  outUV   = mix(c1.ga, c2.ga, f.x);
  99. #else //BGRA = UYVY
  100.   float leftY   = mix(c1.g, c1.a, f.x * 2.0);
  101.   float rightY  = mix(c1.a, c2.g, f.x * 2.0 - 1.0);
  102.   vec2  outUV   = mix(c1.br, c2.br, f.x);
  103. #endif //XBMC_YUY2
  104.  
  105.   float outY    = mix(leftY, rightY, step(0.5, f.x));
  106.  
  107.   vec4  yuv     = vec4(outY, outUV, 1.0);
  108.   vec4  rgb     = m_yuvmat * yuv;
  109.  
  110.   gl_FragColor   = rgb;
  111.   gl_FragColor.a = gl_Color.a;
  112.  
  113. #endif
  114. }
  115.